home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / contrib / pdcurs22 / src / portable / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-26  |  10.2 KB  |  300 lines

  1. /*
  2. ***************************************************************************
  3. * This file comprises part of PDCurses. PDCurses is Public Domain software.
  4. * You may use this code for whatever purposes you desire. This software
  5. * is provided AS IS with NO WARRANTY whatsoever.
  6. * Should this software be used in another application, an acknowledgement
  7. * that PDCurses code is used would be appreciated, but is not mandatory.
  8. *
  9. * Any changes which you make to this software which may improve or enhance
  10. * it, should be forwarded to the current maintainer for the benefit of 
  11. * other users.
  12. *
  13. * The only restriction placed on this code is that no distribution of
  14. * modified PDCurses code be made under the PDCurses name, by anyone
  15. * other than the current maintainer.
  16. * See the file maintain.er for details of the current maintainer.
  17. ***************************************************************************
  18. */
  19. #define    CURSES_LIBRARY    1
  20. #include <curses.h>
  21.  
  22. #if defined(DOS) && defined(MSC)
  23. #include <time.h>
  24. #endif
  25.  
  26. #ifdef OS2
  27. # ifdef EMXVIDEO
  28. #include <termios.h>
  29. # else
  30.     APIRET APIENTRY DosSleep(ULONG ulTime);
  31. # endif
  32. #endif
  33.  
  34. /* undefine any macros for functions defined in this module */
  35. #undef    unctrl
  36. #undef    keyname
  37. #undef    filter
  38. #undef    use_env
  39. #undef    put_win
  40. #undef    get_win
  41. #undef    delay_output
  42. #undef    flushinp
  43.  
  44. /* undefine any macros for functions called by this module if in debug mode */
  45. #ifdef PDCDEBUG
  46. #endif
  47.  
  48. #ifdef PDCDEBUG
  49. char *rcsid_util  = "$Id$";
  50. #endif
  51.  
  52. /*man-start*********************************************************************
  53.  
  54.   Name:                                                          util
  55.  
  56.   Synopsis:
  57.       char *unctrl(chtype c);
  58.       char *keyname(int key);
  59.   ***    int filter(void);
  60.   ***    void use_env(void);
  61.   ***    int putwin(WINDOW *win, FILE *filep);
  62.   ***    WINDOW *getwin(FILE *filep);
  63.       int delay_output( int ms );
  64.       int flushinp(void);
  65.  
  66.   X/Open Description:
  67.      The unctrl() routine expands the character c into a character
  68.      string which is a printable representation of the character.
  69.      Control characters are displayed in the ^X notation.  Printing
  70.      characters are displayed normally.
  71.  
  72.      The keyname() function returns a pointer to a character string 
  73.      containing a symbolic name corresponding to that specified in 
  74.      the argument key. key may be any key returned by wgetch().
  75.  
  76.      The delay_output() function inserts ms millisecond pause in output.
  77.      On some systems, this has no effect.
  78.  
  79.      The flushinp() routine throws away any type-ahead that has been 
  80.      typed by the user and has not yet been read by the program.
  81.  
  82.   PDCurses Description:
  83.      The conversion from a control character to a two-character
  84.      sequence is done by the unctrl() function. In the BSD version
  85.      of curses it is done by a macro, which uses a publicly
  86.      available translation table. Some ill-behaved application
  87.      programs use the table directly, and since it does not exist
  88.      in this curses version such application will link with an
  89.      error message complaining about undefined symbols.
  90.  
  91.      If the PDCurses library is compiled under DOS with the FAST_VIDEO
  92.      define true, then we will poke the BIOS keyboard buffer head and
  93.      tail pointers, resetting the typeahead to implement flushinp().
  94.      If this is not true, then we will be unable to reliably flush
  95.      the typeahead.
  96.  
  97.   X/Open Return Value:
  98.      All functions return OK on success and ERR on error.
  99.  
  100.   X/Open Errors:
  101.      No errors are defined for this function.
  102.  
  103.   Portability                             X/Open    BSD    SYS V
  104.                                           Dec '88
  105.       unctrl                                Y        Y       Y
  106.       keyname                               Y        -      3.0
  107.       filter                                Y        -      3.0
  108.       use_env                               -        -      4.0
  109.       put_win                               -        -      4.0
  110.       get_win                               -        -      4.0
  111.       delay_output                          Y        Y       Y
  112.       flushinp                              Y        Y       Y
  113.  
  114. **man-end**********************************************************************/
  115.  
  116. static char strbuf[3] = {0, 0, 0};
  117.  
  118. /***********************************************************************/
  119. char*    unctrl(chtype c)
  120. /***********************************************************************/
  121. {
  122.     chtype    ic = c;
  123.  
  124. #ifdef PDCDEBUG
  125.     if (trace_on) PDC_debug("unctrl() - called\n");
  126. #endif
  127.  
  128.     ic &= A_CHARTEXT;
  129.     if (ic >= 0x20 && ic != 0x7f)        /* normal characters */
  130.     {
  131.         strbuf[0] = (char) ic;
  132.         strbuf[1] = '\0';
  133.         return( strbuf );
  134.     }
  135.     strbuf[0] = '^';    /* '^' prefix */
  136.     if (c == 0x7f)
  137.     {
  138.         /*
  139.          * 0x7f == DEL
  140.          */
  141.         strbuf[1] = '?';
  142.     }
  143.     else
  144.     {
  145.         /*
  146.          * other control
  147.          */
  148.         strbuf[1] = (char)(ic + '@');
  149.     }
  150.     return( strbuf );
  151. }
  152. /***********************************************************************/
  153. char *    keyname(int key)
  154. /***********************************************************************/
  155. {
  156.     static char *key_name[] =
  157.     {
  158.  "KEY_BREAK","KEY_DOWN","KEY_UP","KEY_LEFT","KEY_RIGHT","KEY_HOME","KEY_BACKSPACE",
  159.  "KEY_F0","KEY_F(1)","KEY_F(2)","KEY_F(3)","KEY_F(4)","KEY_F(5)",
  160.  "KEY_F(6)","KEY_F(7)","KEY_F(8)","KEY_F(9)","KEY_F(10)",
  161.  "KEY_F(11)","KEY_F(12)","KEY_F(13)","KEY_F(14)","KEY_F(15)",
  162.  "KEY_F(16)","KEY_F(17)","KEY_F(18)","KEY_F(19)","KEY_F(20)",
  163.  "KEY_F(21)","KEY_F(22)","KEY_F(23)","KEY_F(24)","KEY_F(25)",
  164.  "KEY_F(26)","KEY_F(27)","KEY_F(28)","KEY_F(29)","KEY_F(30)",
  165.  "KEY_F(31)","KEY_F(32)","KEY_F(33)","KEY_F(34)","KEY_F(35)",
  166.  "KEY_F(36)","KEY_F(37)","KEY_F(38)","KEY_F(39)","KEY_F(40)",
  167.  "KEY_F(41)","KEY_F(42)","KEY_F(43)","KEY_F(44)","KEY_F(45)",
  168.  "KEY_F(46)","KEY_F(47)","KEY_F(48)","KEY_F(49)","KEY_F(50)",
  169.  "KEY_F(51)","KEY_F(52)","KEY_F(53)","KEY_F(54)","KEY_F(55)",
  170.  "KEY_F(56)","KEY_F(57)","KEY_F(58)","KEY_F(59)","KEY_F(60)",
  171.  "KEY_F(61)","KEY_F(62)","KEY_F(63)",
  172.  "KEY_DL","KEY_IL","KEY_DC","KEY_IC","KEY_EIC","KEY_CLEAR","KEY_EOS","KEY_EOL",
  173.  "KEY_SF","KEY_SR","KEY_NPAGE","KEY_PPAGE","KEY_STAB","KEY_CTAB","KEY_CATAB",
  174.  "KEY_ENTER","KEY_SRESET","KEY_RESET","KEY_PRINT","KEY_LL","KEY_ABORT","KEY_SHELP",
  175.  "KEY_LHELP","KEY_BTAB","KEY_BEG","KEY_CANCEL","KEY_CLOSE","KEY_COMMAND","KEY_COPY",
  176.  "KEY_CREATE","KEY_END","KEY_EXIT","KEY_FIND","KEY_HELP","KEY_MARK","KEY_MESSAGE",
  177.  "KEY_MOVE","KEY_NEXT","KEY_OPEN","KEY_OPTIONS","KEY_PREVIOUS","KEY_REDO",
  178.  "KEY_REFERENCE","KEY_REFRESH","KEY_REPLACE","KEY_RESTART","KEY_RESUME","KEY_SAVE",
  179.  "KEY_SBEG","KEY_SCANCEL","KEY_SCOMMAND","KEY_SCOPY","KEY_SCREATE","KEY_SDC","KEY_SDL",
  180.  "KEY_SELECT","KEY_SEND","KEY_SEOL","KEY_SEXIT","KEY_SFIND","KEY_SHOME","KEY_SIC",
  181.  "NO KEY NAME",
  182.  "KEY_SLEFT","KEY_SMESSAGE","KEY_SMOVE","KEY_SNEXT","KEY_SOPTIONS","KEY_SPREVIOUS",
  183.  "KEY_SPRINT","KEY_SREDO","KEY_SREPLACE","KEY_SRIGHT","KEY_SRSUME","KEY_SSAVE",
  184.  "KEY_SSUSPEND","KEY_SUNDO","KEY_SUSPEND","KEY_UNDO",
  185.  "ALT_0","ALT_1","ALT_2","ALT_3","ALT_4","ALT_5","ALT_6","ALT_7","ALT_8","ALT_9",
  186.  "ALT_A","ALT_B","ALT_C","ALT_D","ALT_E","ALT_F","ALT_G","ALT_H","ALT_I","ALT_J",
  187.  "ALT_K","ALT_L","ALT_M","ALT_N","ALT_O","ALT_P","ALT_Q","ALT_R","ALT_S","ALT_T",
  188.  "ALT_U","ALT_V","ALT_W","ALT_X","ALT_Y","ALT_Z","CTL_LEFT","CTL_RIGHT","CTL_PGUP",
  189.  "CTL_PGDN","CTL_HOME","CTL_END","KEY_BACKTAB","KEY_A1","KEY_A2","KEY_A3","KEY_B1",
  190.  "KEY_B2","KEY_B3","KEY_C1","KEY_C2","KEY_C3","PADSLASH","PADENTER","CTL_PADENTER",
  191.  "ALT_PADENTER","SHF_PADSTOP","PADSTAR","PADMINUS","PADPLUS","CTL_PADSTOP",
  192.  "CTL_PADCENTER","CTL_PADPLUS","CTL_PADMINUS","CTL_PADSLASH","CTL_PADSTAR","ALT_PADPLUS",
  193.  "ALT_PADMINUS","ALT_PADSLASH","ALT_PADSTAR","CTL_INS","ALT_DEL","ALT_INS","CTL_UP",
  194.  "CTL_DOWN","CTL_TAB","ALT_TAB","ALT_MINUS","ALT_EQUAL","ALT_HOME","ALT_PGUP","ALT_PGDN",
  195.  "ALT_END","ALT_UP","ALT_DOWN","ALT_RIGHT","ALT_LEFT","ALT_ENTER","ALT_ESC","ALT_BQUOTE",
  196.  "ALT_LBRACKET","ALT_RBRACKET","ALT_SEMICOLON","ALT_FQUOTE","ALT_COMMA","ALT_STOP",
  197.  "ALT_FSLASH","ALT_BKSP","CTL_BKSP","CTL_PAD0","CTL_PAD1","CTL_PAD2","CTL_PAD3","CTL_PAD4",
  198.  "CTL_PAD5","CTL_PAD6","CTL_PAD7","CTL_PAD8","CTL_PAD9","CTL_DEL","ALT_BSLASH","CTL_ENTER"
  199.     };
  200.  
  201. #ifdef PDCDEBUG
  202.     if (trace_on) PDC_debug("keyname() - called: key %d\n",key);
  203. #endif
  204.  
  205.     if (key < KEY_MIN
  206.     ||  key > KEY_MAX)
  207.         return((char *)"NO KEY NAME");
  208.     return( key_name[key-KEY_MIN] );
  209. }
  210. /***********************************************************************/
  211. int    delay_output( int ms )
  212. /***********************************************************************/
  213. {
  214. #ifdef PDCDEBUG
  215.     if (trace_on) PDC_debug("delay_output() - called: ms %d\n",ms);
  216. #endif
  217.  
  218. #if (defined(TC) || defined(WATCOMC)) && defined(DOS)
  219.     delay( ms );
  220.     return( OK );
  221. #endif
  222.  
  223. #if    defined(OS2)
  224. # if defined(EMX)
  225.     _sleep2(ms);
  226. # else
  227.     DosSleep(ms);
  228. # endif
  229.     return( OK );
  230. #endif
  231.  
  232. #if    defined(DOS) && defined(MSC)
  233.     PDC_usleep((clock_t)ms);
  234.     return( OK );
  235. #endif
  236.  
  237. #if    defined(DOS) && defined(NDP)
  238.     clock_t goal;
  239.     goal = ms + (float)( (float)clock()/(float)CLOCKS_PER_SEC )*1000;
  240.     while (goal > (float)( (float)clock()/(float)CLOCKS_PER_SEC )*1000)
  241.     ;
  242.     return( OK );
  243. #endif
  244.  
  245. #if defined(UNIX) || defined(GO32)
  246.     usleep(1000*ms);
  247.     return( OK );
  248. #endif
  249.  
  250. #if defined(XCURSES)
  251.     PDC_usleep(ms);
  252.     return( OK );
  253. #endif
  254.  
  255. }
  256. /***********************************************************************/
  257. int    flushinp(void)
  258. /***********************************************************************/
  259. {
  260. extern int    c_pindex;        /* putter index */
  261. extern int    c_gindex;        /* getter index */
  262. extern int    c_ungind;        /* wungetch() push index */
  263.  
  264. #ifdef PDCDEBUG
  265.     if (trace_on) PDC_debug("flushinp() - called\n");
  266. #endif
  267.  
  268. #if defined(DOS) && defined(FAST_VIDEO)
  269.     setdosmemword (0x41a, getdosmemword (0x41c)); /* Force the BIOS kbd buf       */
  270.                     /* head/tail pointers to be the */
  271.                     /* same...  Real nasty trick... */
  272. #  if defined(NDP)
  273. /*
  274.     int *KB_HEAD = (int *) mapdev( 0x041aL, sizeof(short) );
  275.     int *KB_TAIL = (int *) mapdev( 0x041cL, sizeof(short) );
  276.  
  277.     memcpy( KB_HEAD, KB_TAIL, sizeof(short) );
  278. */
  279. #  endif
  280. #endif
  281.  
  282. #ifdef OS2
  283. #  ifdef EMXVIDEO
  284.     tcflush(0,TCIFLUSH);
  285. #  else
  286.     KbdFlushBuffer(0);
  287. #  endif
  288. #endif
  289.  
  290. #ifdef UNIX
  291. /* INCOMPLETE */
  292. #endif
  293.  
  294.     c_gindex = 1;            /* set indices to kill buffer     */
  295.     c_pindex = 0;
  296.     c_ungind = 0;            /* clear c_ungch array         */
  297.     return( OK );
  298. }
  299.